<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class MirrorController extends Controller
	{
		/*
		 *个人写真/企业风采 
		 * 还未加上上传照片的时间判断  
		 */
		public function userMirror($uid=null, $token=null)
		{
// 			if ($this->checkedToken($uid, $token)) 
// 			{
				$where["uid"] = $uid;
				$userinfo = M("User") -> field("uid,type,rmtype") -> where($where) -> find();
				if ($userinfo["type"] == 0)
				{
					//个人写真
					if ($userinfo["rmtype"] == 1) 
					{
						//个人注册后已完善资料
						$where1["uid"] = $uid;
						$userMirror = M("Userinfo") -> where($where1) -> getField("photo");
						//var_dump($userMirror);
						if ($userMirror != null) 
						{
							$userMirrorInfo = explode(",", $userMirror);
						}
						else
						{
							$userMirrorInfo = array();
						}
						
						//var_dump($userMirrorInfo);
						$array1['result'] = 0;
						$array1['message'] = "获取写真成功";
						$array1["usermirrorinfo"] = $userMirrorInfo;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						//用户注册后未完善个人资料
						$userMirrorInfo = array();
						
						$array1['result'] = 0;
						$array1['message'] = "获取写真成功";
						$array1["usermirrorinfo"] = $userMirrorInfo;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				else
				{
					//企业风采
					if ($userinfo["rmtype"] == 1) 
					{
						//企业注册后已完善资料
						$where1["uid"] = $uid;
						$userMirror = M("Company") -> where($where1) -> getField("photo");
						//var_dump($userMirror);
						if ($userMirror != null)
						{
							$userMirrorInfo = explode(",", $userMirror);
						}
						else
						{
							$userMirrorInfo = array();
						}
						
						//var_dump($userMirrorInfo);
						$array1['result'] = 0;
						$array1['message'] = "获取写真成功";
						$array1["usermirrorinfo"] = $userMirrorInfo;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						//企业注册后未完善资料
						$userMirrorInfo = array();
						
						$array1['result'] = 0;
						$array1['message'] = "获取写真成功";
						$array1["usermirrorinfo"] = $userMirrorInfo;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
// 			}
// 			else
// 			{
// 				$array1['result'] = 10310;
// 				$array1['message'] = "获取信息失败";
// 				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
// 				exit;
// 			}
			
		}
		
		/*
		 *个人视频
		 *
		 */
		public function userVedio($uid=null, $token=null)
		{
			if ($this->checkedToken($uid, $token)) 
			{
				$where["uid"] = $uid;
				$userinfo = M("User") -> field("uid,type,rmtype") -> where($where) -> find();
				//个人写真
				if ($userinfo["rmtype"] == 1)
				{
					//个人注册后已完善资料
					$where1["uid"] = $uid;
					$userVedio = M("Userinfo") -> where($where1) -> getField("vedio");
					//var_dump($userVedio);
					if ($userVedio != null)
					{
						$userVedioInfo = explode(",", $userVedio);
					}
					else
					{
						$userVedioInfo = array();
					}
			
					//var_dump($userVedioInfo);
					$array1['result'] = 0;
					$array1['message'] = "获取视频成功";
					$array1["uservedioinfo"] = $userVedioInfo;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					//用户注册后未完善个人资料
					$userVedioInfo = "";
			
					$array1['result'] = 0;
					$array1['message'] = "获取视频成功";
					$array1["uservedioinfo"] = $userVedioInfo;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
			}
			else
			{
				$array1['result'] = 10310;
				$array1['message'] = "获取视频失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
		
		/*
		 *设置背景图
		 *
		 */
		public function setBackground($uid=null, $token=null, $backgroundimg=null)
		{
			if ($this->checkedToken($uid, $token)) 
			{
				if ($backgroundimg == null)
				{
					$array1['result'] = 10511;
					$array1['message'] = "背景图设置失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				elseif ($_REQUEST['backgroundimg'] == base64_encode(base64_decode($_REQUEST['backgroundimg'])))
				{
					//从手机端上传的背景图
					$img = base64_decode($_REQUEST['backgroundimg']);
					$oldname = $_REQUEST['oldname'];
					$index = strrpos($oldname, ".");//对文件名进行“.”截取
					$ext = substr($oldname, $index);//获得文件的扩展名
					$newname = md5(time() . rand(0, 9999)).$ext;
					$result = file_put_contents('./Public/Uploads/Headimg/'.$newname, $img);
					$data["background"] = "http://192.168.1.119/loveshow/Public/Uploads/Background/".$newname;
					
					
					$condition["uid"] = $uid;
					$typeInfo = M("User") -> field("type，rmtype") -> where($condition) -> find();
					if ($typeInfo["type"] == 0) 
					{
						//个人背景图
						if ($typeInfo["rmtype"] == 1) 
						{
							//已完善过资料
							$backgroundInfo = M("Userinfo") -> field("background") -> where($condition) -> find();
							if ($backgroundInfo == null)
							{
								//已完善过资料且首次上传背景图
								$res = M("Userinfo") -> where($condition) -> setField("background", $data);
							}
							else
							{
								//更改个人用户背景
								$backgroundArray = explode("/", $backgroundInfo["background"]);
// 								$headimgName = substr($result["headimg"], -36);
								$headimgName = end($backgroundArray);
								$delResult = unlink("./Public/Uploads/Headimg/".$headimgName);
								
								$res = M("Userinfo") -> where($condition) -> setField("background", $data);
							}
						}
						else
						{
							//该用户未完善过资料
							$data1 = array(
									"uid"=>$uid,
									"background"=>$data["background"]
							);
							$res = M("Userinfo") -> data($data1) -> add();
						}
						
					}
					else
					{
						//企业背景图
						if ($typeInfo["rmtype"] == 1)
						{
							//已完善过资料
							$backgroundInfo = M("Company") -> field("background") -> where($condition) -> find();
							if ($backgroundInfo == null)
							{
								//已完善过资料且首次上传背景图
								$res = M("Company") -> where($condition) -> setField("background", $data);
							}
							else
							{
								//更改个人用户背景
								$backgroundArray = explode("/", $backgroundInfo["background"]);
// 								$headimgName = substr($result["headimg"], -36);
								$headimgName = end($backgroundArray);
								$delResult = unlink("./Public/Uploads/Headimg/".$headimgName);
								
								$res = M("Company") -> where($condition) -> setField("background", $data);
							}
						}
						else
						{
							//该用户未完善过资料
							$data1 = array(
									"uid"=>$uid,
									"background"=>$data["background"]
							);
							$res = M("Company") -> data($data1) -> add();
						}
					}
				}
				else
				{
					//从写真中上传背景图
					
				
				}
				
				if ($res > 0)
				{
					$array1['result'] = 0;
					$array1['message'] = "背景图设置成功";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$array1['result'] = 10512;
					$array1['message'] = "背景图设置失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
			}
			else
			{
				$array1['result'] = 10310;
				$array1['message'] = "获取视频失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
			
		}
		
		/*
		 *验证token的方法
		 *
		 */
		protected function checkedToken($uid = null,$token = null)
		{
			if ($uid==null || $token==null)
			{
				return false;
			}
			else
			{
				$where["uid"] = $uid;
				$tokeninfo = M("User") -> where($where) -> getField("token");
				if ($token == $tokeninfo)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}